package com.kelaocai.motto.utility;

public class AutoGenDaoCode {

	/**
	 * 根据指定的Pojo生成对应DAO中映射部分代码 eg:PoJo类EbsTdApl.areano 生成后代码：
	 * ebs_td_apl.setAreano(Pub.exStr_Null(rs.getString("areano")));
	 * 
	 * @param args
	 */
	
	
	public static void main(String[] args) {
		
		final String CLASS_NAME="com.kelaocai.motto.entity.Motto";
		
		try {
			// 指定需要转换的类名，包括完整的包名
			
			Class entity = Class
					.forName(CLASS_NAME);
			String instancename = split_parameter(entity.getSimpleName());
			String exstr_Null = "";
			for (int i = 0; i < entity.getMethods().length; i++) {
				String methodname = entity.getMethods()[i].getName();
				if (methodname.substring(0, 3).equals("set")) {
					String field = methodname.substring(3).toLowerCase();
					String mothed = entity.getMethods()[i].getName();
					String valuetype = entity.getMethods()[i]
							.getParameterTypes()[0].getSimpleName();
					String rsmothed = " cursor.getString(cursor.getColumnIndex"
							+ valuetype.substring(0, 1).toUpperCase()
							+ valuetype.substring(1);
					String statement = "";
					if (valuetype.equals("String")) {
						if (!exstr_Null.equals(""))
							statement = instancename + "." + mothed + "("
									+ exstr_Null + "(" + rsmothed + "(\""
									+ field + "\")));";
						else
							statement = instancename + "." + mothed + "("
									+ rsmothed + "(\"" + field + "\"));";
					} else
						statement = instancename + "." + mothed + "("
								+ rsmothed + "(\"" + field + "\"));";

					System.out.println(statement);
				}
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static String split_parameter(String parameter) {

		String tmp = "";
		int j = parameter.length();
		for (int i = 0; i < j; i++) {
			tmp += parameter.substring(i, i + 1);
			if (i + 1 != j
					&& parameter.substring(i + 1, i + 2).toUpperCase().equals(
							parameter.substring(i + 1, i + 2))) {
				tmp += "_";
			}
		}

		return tmp.toLowerCase();
	}

}
